export type Locale = "en" | "zh-Hans" | "zh-Hant";

type TranslationDict = Record<string, string>;

const base: TranslationDict = {
  title: "rtp2httpd Status",
  uptime: "Uptime",
  totalClients: "Active Clients",
  totalBandwidth: "Total Bandwidth",
  totalTraffic: "Total Traffic",
  maxClients: "Max Clients",
  workerStats: "Worker Statistics",
  connections: "Connections",
  noConnections: "No connections",
  noWorkerStats: "No worker statistics available.",
  client: "Client",
  service: "Service",
  state: "State",
  duration: "Duration",
  bandwidth: "Bandwidth",
  dataSent: "Data Sent",
  queueDrops: "Queue & Drops",
  action: "Action",
  disconnect: "Disconnect",
  disconnecting: "Disconnecting…",
  showDisconnected: "Show disconnected clients",
  logs: "Logs",
  logLevel: "Log level",
  autoScroll: "Auto-scroll",
  connected: "Connected to rtp2httpd",
  disconnected: "Disconnected",
  reconnecting: "Reconnecting…",
  workerPid: "Worker PID",
  slowClient: "Slow client",
  bufferPool: "Buffer pool",
  controlPool: "Control pool",
  sendStats: "Send stats",
  sendTotal: "Total Sends",
  sendCompletions: "Completions",
  sendCopied: "Copied",
  sendEagain: "EAGAIN",
  sendEnobufs: "ENOBUFS",
  sendBatch: "Batch flushes",
  poolTotal: "Total",
  poolFree: "Free",
  poolUsed: "Used",
  poolMax: "Max",
  poolExpansions: "Expansions",
  poolExhaustions: "Exhaustions",
  clientsPerWorker: "Clients",
  language: "Language",
  appearance: "Appearance",
  themeAuto: "Auto",
  themeLight: "Light",
  themeDark: "Dark",
  lastUpdated: "Last updated",
  queueUsage: "Queue usage",
  version: "Version",
  queueCurrent: "Queued",
  queueLimit: "Limit",
  queuePeak: "Peak usage",
  queueDroppedBytes: "Dropped bytes",
  clientStateConnecting: "Connecting",
  clientStateError: "Error",
  clientStateFccInit: "FCC Init",
  clientStateFccRequested: "FCC Requested",
  clientStateFccUnicastPending: "FCC Unicast Pending",
  clientStateFccUnicastActive: "FCC Unicast Active",
  clientStateFccMcastRequested: "FCC Multicast Requested",
  clientStateFccMcastActive: "Multicast Active",
  clientStateRtspInit: "RTSP Init",
  clientStateRtspConnecting: "RTSP Connecting",
  clientStateRtspConnected: "RTSP Connected",
  clientStateRtspSendingOptions: "RTSP Sending OPTIONS",
  clientStateRtspAwaitingOptions: "RTSP Awaiting OPTIONS",
  clientStateRtspSendingDescribe: "RTSP Sending DESCRIBE",
  clientStateRtspAwaitingDescribe: "RTSP Awaiting DESCRIBE",
  clientStateRtspDescribed: "RTSP Described",
  clientStateRtspSendingSetup: "RTSP Sending SETUP",
  clientStateRtspAwaitingSetup: "RTSP Awaiting SETUP",
  clientStateRtspSetup: "RTSP Setup",
  clientStateRtspSendingPlay: "RTSP Sending PLAY",
  clientStateRtspAwaitingPlay: "RTSP Awaiting PLAY",
  clientStateRtspPlaying: "RTSP Playing",
  clientStateRtspReconnecting: "RTSP Reconnecting",
  clientStateRtspSendingTeardown: "RTSP Sending TEARDOWN",
  clientStateRtspAwaitingTeardown: "RTSP Awaiting TEARDOWN",
  clientStateRtspTeardownComplete: "RTSP Teardown Complete",
  clientStateRtspPaused: "RTSP Paused",
  clientStateDisconnected: "Disconnected",
  clientStateUnknown: "Unknown State",
};

const zhHans: TranslationDict = {
  title: "rtp2httpd 状态",
  uptime: "运行时间",
  totalClients: "活跃连接",
  totalBandwidth: "总带宽",
  totalTraffic: "累计流量",
  maxClients: "最大连接数",
  workerStats: "工作进程统计",
  connections: "连接",
  noConnections: "暂无连接",
  noWorkerStats: "暂无工作进程统计",
  client: "客户端",
  service: "服务",
  state: "状态",
  duration: "时长",
  bandwidth: "带宽",
  dataSent: "已发送",
  queueDrops: "队列与丢包",
  action: "操作",
  disconnect: "断开",
  disconnecting: "正在断开…",
  showDisconnected: "显示已断开连接",
  logs: "日志",
  logLevel: "日志级别",
  autoScroll: "自动滚动",
  connected: "已连接到 rtp2httpd",
  disconnected: "已断开",
  reconnecting: "重新连接中…",
  workerPid: "工作进程 PID",
  slowClient: "慢客户端",
  bufferPool: "缓冲池",
  controlPool: "控制池",
  sendStats: "发送统计",
  sendTotal: "总发送次数",
  sendCompletions: "完成次数",
  sendCopied: "拷贝次数",
  sendEagain: "EAGAIN 次数",
  sendEnobufs: "ENOBUFS 次数",
  sendBatch: "批量刷新",
  poolTotal: "总量",
  poolFree: "空闲",
  poolUsed: "已用",
  poolMax: "最大值",
  poolExpansions: "扩容次数",
  poolExhaustions: "耗尽次数",
  clientsPerWorker: "连接数",
  language: "语言",
  appearance: "外观",
  themeAuto: "自动",
  themeLight: "浅色",
  themeDark: "深色",
  lastUpdated: "更新时间",
  queueUsage: "队列占用",
  version: "版本",
  queueCurrent: "当前占用",
  queueLimit: "队列上限",
  queuePeak: "峰值占用",
  queueDroppedBytes: "丢弃字节",
  clientStateConnecting: "连接中",
  clientStateError: "错误",
  clientStateFccInit: "FCC 初始化",
  clientStateFccRequested: "FCC 已请求",
  clientStateFccUnicastPending: "FCC 单播等待中",
  clientStateFccUnicastActive: "FCC 单播活跃",
  clientStateFccMcastRequested: "FCC 组播已请求",
  clientStateFccMcastActive: "组播活跃",
  clientStateRtspInit: "RTSP 初始化",
  clientStateRtspConnecting: "RTSP 连接中",
  clientStateRtspConnected: "RTSP 已连接",
  clientStateRtspSendingOptions: "RTSP 发送 OPTIONS",
  clientStateRtspAwaitingOptions: "RTSP 等待 OPTIONS",
  clientStateRtspSendingDescribe: "RTSP 发送 DESCRIBE",
  clientStateRtspAwaitingDescribe: "RTSP 等待 DESCRIBE",
  clientStateRtspDescribed: "RTSP 已 DESCRIBE",
  clientStateRtspSendingSetup: "RTSP 发送 SETUP",
  clientStateRtspAwaitingSetup: "RTSP 等待 SETUP",
  clientStateRtspSetup: "RTSP 已 SETUP",
  clientStateRtspSendingPlay: "RTSP 发送 PLAY",
  clientStateRtspAwaitingPlay: "RTSP 等待 PLAY",
  clientStateRtspPlaying: "RTSP 播放中",
  clientStateRtspReconnecting: "RTSP 重新连接",
  clientStateRtspSendingTeardown: "RTSP 发送 TEARDOWN",
  clientStateRtspAwaitingTeardown: "RTSP 等待 TEARDOWN",
  clientStateRtspTeardownComplete: "RTSP 完成 TEARDOWN",
  clientStateRtspPaused: "RTSP 暂停",
  clientStateDisconnected: "已断开",
  clientStateUnknown: "未知状态",
};

const zhHant: TranslationDict = {
  title: "rtp2httpd 狀態",
  uptime: "運行時間",
  totalClients: "活躍連線",
  totalBandwidth: "總頻寬",
  totalTraffic: "累計流量",
  maxClients: "最大連線數",
  workerStats: "工作行程統計",
  connections: "連線",
  noConnections: "暫無連線",
  noWorkerStats: "暫無工作行程統計",
  client: "客戶端",
  service: "服務",
  state: "狀態",
  duration: "時長",
  bandwidth: "頻寬",
  dataSent: "已傳送",
  queueDrops: "佇列與丟包",
  action: "操作",
  disconnect: "中斷",
  disconnecting: "正在中斷…",
  showDisconnected: "顯示已中斷連線",
  logs: "日誌",
  logLevel: "日誌等級",
  autoScroll: "自動捲動",
  connected: "已連線到 rtp2httpd",
  disconnected: "已斷線",
  reconnecting: "重新連線中…",
  workerPid: "工作行程 PID",
  slowClient: "慢速客戶端",
  bufferPool: "緩衝池",
  controlPool: "控制池",
  sendStats: "傳送統計",
  sendTotal: "總傳送次數",
  sendCompletions: "完成次數",
  sendCopied: "拷貝次數",
  sendEagain: "EAGAIN 次數",
  sendEnobufs: "ENOBUFS 次數",
  sendBatch: "批次刷新",
  poolTotal: "總量",
  poolFree: "空閒",
  poolUsed: "已用",
  poolMax: "最大值",
  poolExpansions: "擴充次數",
  poolExhaustions: "耗盡次數",
  clientsPerWorker: "連線數",
  language: "語言",
  appearance: "外觀",
  themeAuto: "自動",
  themeLight: "淺色",
  themeDark: "深色",
  lastUpdated: "更新時間",
  queueUsage: "佇列使用率",
  version: "版本",
  queueCurrent: "目前占用",
  queueLimit: "佇列上限",
  queuePeak: "峰值占用",
  queueDroppedBytes: "丟棄位元組",
  clientStateConnecting: "連線中",
  clientStateError: "錯誤",
  clientStateFccInit: "FCC 初始化",
  clientStateFccRequested: "FCC 已請求",
  clientStateFccUnicastPending: "FCC 單播待命",
  clientStateFccUnicastActive: "FCC 單播活躍",
  clientStateFccMcastRequested: "FCC 多播已請求",
  clientStateFccMcastActive: "多播活躍",
  clientStateRtspInit: "RTSP 初始化",
  clientStateRtspConnecting: "RTSP 連線中",
  clientStateRtspConnected: "RTSP 已連線",
  clientStateRtspSendingOptions: "RTSP 傳送 OPTIONS",
  clientStateRtspAwaitingOptions: "RTSP 等待 OPTIONS",
  clientStateRtspSendingDescribe: "RTSP 傳送 DESCRIBE",
  clientStateRtspAwaitingDescribe: "RTSP 等待 DESCRIBE",
  clientStateRtspDescribed: "RTSP 已完成 DESCRIBE",
  clientStateRtspSendingSetup: "RTSP 傳送 SETUP",
  clientStateRtspAwaitingSetup: "RTSP 等待 SETUP",
  clientStateRtspSetup: "RTSP 已 SETUP",
  clientStateRtspSendingPlay: "RTSP 傳送 PLAY",
  clientStateRtspAwaitingPlay: "RTSP 等待 PLAY",
  clientStateRtspPlaying: "RTSP 播放中",
  clientStateRtspReconnecting: "RTSP 重新連線",
  clientStateRtspSendingTeardown: "RTSP 傳送 TEARDOWN",
  clientStateRtspAwaitingTeardown: "RTSP 等待 TEARDOWN",
  clientStateRtspTeardownComplete: "RTSP 完成 TEARDOWN",
  clientStateRtspPaused: "RTSP 暫停",
  clientStateDisconnected: "已斷線",
  clientStateUnknown: "未知狀態",
};

export const translations: Record<Locale, TranslationDict> = {
  en: base,
  "zh-Hans": { ...base, ...zhHans },
  "zh-Hant": { ...base, ...zhHant },
};

export type TranslationKey = keyof typeof base;

export function translate(locale: Locale, key: TranslationKey): string {
  return translations[locale][key] ?? base[key];
}
